/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.oea.hibernate.entities;

import com.oea.hibernate.utilities.HibernateUtil;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author unnati
 */
@Table(name = "results")
@Entity
public class Result extends AbstractEntity 
{
    @GeneratedValue
    @Id
    
 @Column(name = "id")
    protected  Long id;

    
    @Column(name = "sid")
    protected Long sid;
    
    @Column(name="tid")
    protected Long tid;
    
    @Column(name="status")
    protected boolean  status;
    
    @Column(name = "canswer")
    protected int canswer;
    
    @Column(name = "wanswer")
    protected int wanswer;
    
    @Column(name = "skipans")
    protected  int skipans;

    @Transient
    protected  Student student;
            
    @Transient 
    protected Test test;

    public int getSkipans() {
        return skipans;
    }

    public void setSkipans(int skipans) {
        this.skipans = skipans;
    }
    
    

    public Student getStudent() {
        return student;
    }

    public void setStudent() {
        this.student = (Student) new Student().getById(sid);
    }

    public Test getTest() {
        return test;
    }

    public void setTest() {
        this.test = (Test) new Test().getById(tid);
    }
    
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getTid() {
        return tid;
    }

    public void setTid(Long tid) {
        this.tid = tid;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public int getCanswer() {
        return canswer;
    }

    public void setCanswer(int canswer) {
        this.canswer = canswer;
    }

    public int getWanswer() {
        return wanswer;
    }

    public void setWanswer(int wanswer) {
        this.wanswer = wanswer;
    }

    
    public Long getSid() {
        return sid;
    }

    public void setSid(Long sid) {
        this.sid = sid;
    }
    
    public static  List<Result> GetResultByTestId(Long testid)
    {
        List<Result> li_result=null;
        
        try 
        {
            Session s=HibernateUtil.getSessionFactory().openSession();
            Query q=s.createQuery("from Result where tid="+testid+"");
            li_result=q.list();
        }
        catch (Exception e) 
        {
           e.printStackTrace();
        }
        
        return li_result;
                
    }
    public static  List<Result> GetResultByTestOfStudent(Long examid,Long stuid)
    {
        List<Result> li_result=null;
        
        try 
        {
            Session s=HibernateUtil.getSessionFactory().openSession();
            Query q=s.createQuery("from Result where sid="+stuid+" and tid in(select id from Test where eid="+examid+")");
            li_result=q.list();
        } 
        catch (Exception e)
        {
            e.printStackTrace();
        }
        
        return li_result;
    }
    
    public  static  List<Result> GetResultByTestOfClass(Long examid,Long divid)
    {
        List<Result> li_result=null;
        
        try 
        {
            Session s=HibernateUtil.getSessionFactory().openSession();
            Query q=s.createQuery("from Result where sid in(select id from Student where divid="+divid+") and tid in(select id from Test where eid="+examid+")");
            li_result=q.list();
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        return li_result;
    }
    public  static  List<Result> StudentProgressByTest(Long stuid,Long subid)
    {
        List<Result> li_result=null;
        
        try 
        {
            Session s=HibernateUtil.getSessionFactory().openSession();
            Query q=s.createQuery("from Result where sid="+stuid+" and tid in(select id from Test where subid="+subid+")");
            li_result=q.list();
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        return li_result;
    }
    
    public  static  List<Result> ClassProgressByTest(Long divid,Long subid)
    {
        List<Result> li_result=null;
        
        try 
        {
            Session s=HibernateUtil.getSessionFactory().openSession();
            Query q=s.createQuery("from Result where sid in(select id from Student where divid="+divid+") and tid in(select id from Test where subid="+subid+")");
            li_result=q.list();
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        
        return li_result;
    }
}
